# coding:utf-8
'''
 the simple server for 4xDevice
 2018-11-06
'''

import os, sys
import socket, select
import queue
import time
import MyLogger

gLog = MyLogger.getLogger('4xServerDemo')

server_addr = ("10.70.0.38", 9004)
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

#server.setblocking(False)
server.bind(server_addr)
gLog.info("start listen... {0}".format(server_addr))
server.listen(1)

pool = []
pool.append(server)

clientsocket = None
while True:
    # 建立客户端连接
    clientsocket, addr = server.accept()
    if not clientsocket:
        continue
    else:
        gLog.info("连接地址: {0}".format(clientsocket.getpeername()))
        break

while True:
        data = clientsocket.recv(1024)
        if len(data) == 0:
            gLog.info("closed {0}".format(clientsocket.getpeername()))
            server.close()
            break
        else:
            gLog.info("received from {0}".format(clientsocket.getpeername()))
            gLog.info("received data {0}".format(data))
